package zz.app.ustbgsmanager.fragments;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import java.util.ArrayList;
import java.util.Objects;

import zz.app.ustbgsmanager.R;
import zz.app.ustbgsmanager.adapters.CourseFragmentPagerAdapter;
import zz.app.ustbgsmanager.beans.querycourse.CourseInfo;
import zz.app.ustbgsmanager.beans.querycourse.CoursesInTerm;

public class QueryCourseFragment extends Fragment {
    private static final String TAG = "QueryCourseFragment";
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private int selected;
    private Spinner spinner;
    private Spinner currentWeekSpinner;
    private CourseFragmentPagerAdapter adapter;
    private String[] weeks={"全部","一","二","三","四","五","六","七","八","九","十","十一","十二","十三","十四","十五","十六"};

    public ViewPager getViewPager() {
        return viewPager;
    }

    public Spinner getSpinner() {
        return spinner;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_querycourse,container,false);
        spinner=view.findViewById(R.id.spinner);
        currentWeekSpinner=view.findViewById(R.id.spCurrentWeek);
        currentWeekSpinner.setAdapter(new ArrayAdapter<>(Objects.requireNonNull(getContext()),R.layout.term_spinner_layout,weeks));

        tabLayout=view.findViewById(R.id.tabLayout);
        viewPager=view.findViewById(R.id.viewpager_querycourse);
        tabLayout.setupWithViewPager(viewPager);
        viewPager.addOnAdapterChangeListener(new ViewPager.OnAdapterChangeListener() {
            @Override
            public void onAdapterChanged(@NonNull ViewPager viewPager, @Nullable PagerAdapter oldAdapter, @Nullable PagerAdapter newAdapter) {
                viewPager.setCurrentItem(selected,false);
            }
        });
        return view;
    }

    public void setData(final CoursesInTerm coursesInTerm) {
        selected=tabLayout.getSelectedTabPosition();
        adapter=new CourseFragmentPagerAdapter(getFragmentManager(), coursesInTerm);
        viewPager.setAdapter(adapter);
        currentWeekSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(position>0)
                    filterData(coursesInTerm,position);
                else
                    setData(coursesInTerm);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }
    public void filterData(CoursesInTerm coursesInTerm,int week){
        selected=tabLayout.getSelectedTabPosition();
        CoursesInTerm courses=new CoursesInTerm();
        for(ArrayList<CourseInfo> infos:coursesInTerm.getInfos()){
            ArrayList<CourseInfo> infos1=new ArrayList<>();
            for(CourseInfo c:infos){
                int s=c.getStartWeek();
                int e=c.getEndWeek();
                if(s<= week && week <=e){
                    infos1.add(c);
                }
            }
            courses.getInfos().add(infos1);
        }
        adapter=new CourseFragmentPagerAdapter(getFragmentManager(),courses);
        viewPager.setAdapter(adapter);
    }

    public void setTermDefault() {
        currentWeekSpinner.setSelection(0);
    }
}
